package com.xiaomiMall.mange.controller;


import com.alibaba.fastjson.JSON;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.xiaomiMall.mange.domain.FromProd;
import com.xiaomiMall.mange.domain.Prod;
import com.xiaomiMall.mange.service.HomeBodyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/api")
public class HomepageBodyController extends BaseController {

    private String avatar;
    private String avatar1;
    private Integer sign;
    private String likeSelect;
    private AjaxResult ajaxResult;


    @Autowired
    private HomeBodyService homeBodyService;

    @GetMapping("getPhone")
    public String getPhone() {
        List<Prod> selectAll = homeBodyService.selectAll();
        return JSON.toJSONString(selectAll);
    }

    @GetMapping("getProds")
    public AjaxResult getProds() {
        startPage();
        if (sign==null){
            sign=1;
        }
            List<Prod> prods = homeBodyService.selectSign(sign,this.likeSelect);
            Integer count = homeBodyService.selectSignCount(sign,this.likeSelect);
            if (prods == null && count == 0) {
                AjaxResult.error("没有对应的产品");
            }
            this.likeSelect=null;
            ajaxResult = AjaxResult.success();
            ajaxResult.put("prods", prods);
            ajaxResult.put("count", count);
        return ajaxResult;
    }
    @GetMapping("addProd")
    public AjaxResult addProd(FromProd fromProd) {
        if (fromProd != null && avatar != null) {
            fromProd.setAvatar(avatar);
            int i = homeBodyService.addProd(fromProd);
            if (i > 0) {
                Integer id = homeBodyService.selectId(fromProd);
                fromProd.setId(id);
                System.out.println(fromProd);
                int i1 = homeBodyService.addProd_tetail(fromProd);
                if(i1>0){
                return AjaxResult.success("添加成功");
                }
                return AjaxResult.error("添加失败");
            }
        }
        return AjaxResult.error("信息没有上传全");
    }

    @PostMapping("/toFile")
    public AjaxResult toFile(@RequestParam("file") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
            return AjaxResult.success("上传图片成功");
        } else {
            return AjaxResult.error("上传图片异常，请联系管理员");
        }
    }



    @PostMapping("/select")
    public AjaxResult select_id(Prod prod) {
        Prod prod1 = homeBodyService.select_id(prod);
        if (prod1 != null) {
            AjaxResult ajaxResult = AjaxResult.success();
            ajaxResult.put("isSuccess", prod1);
            return ajaxResult;
        } else {
            return AjaxResult.error("用户不存在");
        }
    }

    @PostMapping("/upAndDown")
    public AjaxResult upAndDown(Prod prod) {
        prod.setStand_up_and_down(prod.getStand_up_and_down()== 0 ? 1 : 0);
        int i = homeBodyService.upAndDown(prod);
        if (i != 0) {
            AjaxResult ajaxResult = AjaxResult.success();
            ajaxResult.put("isSuccess", i);
            return ajaxResult;
        } else {
            return AjaxResult.error("删除失败");
        }
    }
    @PostMapping("/updateOne")
    public AjaxResult updateOne(Prod prod) {
        if (avatar1!=null){
            prod.setProd_pic(avatar1);
        }
       if(prod!=null){
           int i = homeBodyService.updateOne(prod);

           if (i>0){   return AjaxResult.success(i);}
       }
        return AjaxResult.error("修改失败");
    }
    @PostMapping("/setClassify")
    public AjaxResult selectTop(Integer sign) {
     this.sign=sign;
     return AjaxResult.success();
    }
    @PostMapping("/likeSelect")
    public AjaxResult likeSelect(String likeSelect) {
         this.likeSelect=likeSelect;
        return AjaxResult.success();
    }
}

